package com.yunxin.admin.system.api.core.customer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunxin.admin.system.domain.model.user.UserMerchantRepository;
import com.yunxin.admin.system.infra.common.constant.SecurityConstants;
import com.yunxin.admin.system.infra.framework.resubmit.Resubmit;
import com.yunxin.admin.system.infra.framework.security.util.SecurityUtils;
import com.yunxin.admin.system.infra.persistence.mapper.SysUserMerchantMapper;
import com.yunxin.common.exception.BusinessException;
import com.yunxin.common.result.PageResult;
import com.yunxin.common.result.Result;
import com.yunxin.core.application.dto.order.OrderUserApplyDetailDTO;
import com.yunxin.core.application.service.TeamService;
import com.yunxin.core.customer.domain.MatchingProduct;
import com.yunxin.core.customer.dto.*;
import com.yunxin.core.customer.service.CustomerInfoService;
import com.yunxin.core.customer.service.MatchingProductService;
import com.yunxin.core.customer.vo.*;
import com.yunxin.core.domain.model.team.Team;
import com.yunxin.core.domain.model.team.UserTeamShip;
import com.yunxin.core.infra.persistence.mapper.user.UserTeamShipMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zero°
 * @since 2023-06-08
 */
@Tag(name = "11.客户管理")
@Slf4j
@RestController
@RequestMapping("/core/v1/matching")
@RequiredArgsConstructor
public class MatchingProductController {

    @Autowired
    private MatchingProductService matchingProductService;


    @Operation(summary = "获取团队分配产品下拉框", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping("/getMatchingProductList")
    public Result<List<MatchingProduct>> getMatchingProductList(@RequestParam(value = "teamId") Integer teamId) {
        List<MatchingProduct> matchingProductList = matchingProductService.getMatchingProductList(teamId);
        return Result.success(matchingProductList);
    }

    @Operation(summary = "新增分配产品", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PostMapping("/add")
    @PreAuthorize("@ss.hasPerm('boke:matching:add')")
    public Result add(@Validated @RequestBody MatchingProduct matchingProduct)  {
        Long userId = SecurityUtils.X.getUser().getUserId();
        List<Integer> teamIds = matchingProduct.getTeamIds();
        List<MatchingProduct> matchingProductList=new ArrayList<>();
        for (Integer teamId : teamIds) {
            matchingProduct.setId(null);
            matchingProduct.setTeamId(teamId);
            matchingProduct.setCreator(userId.toString());
            matchingProduct.setCreateTime(new Date());
            matchingProductList.add(matchingProduct);
        }
        matchingProductService.saveBatch(matchingProductList);
        return Result.success();
    }

    @Operation(summary = "修改分配产品", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPerm('boke:matching:update')")
    public Result update(@Validated @RequestBody MatchingProduct matchingProduct)  {
        Long userId = SecurityUtils.X.getUser().getUserId();
        matchingProduct.setEditor(userId.toString());
        matchingProduct.setEditTime(new Date());
        matchingProductService.updateById(matchingProduct);
        return Result.success();
    }

    @Operation(summary = "删除分配产品", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @DeleteMapping("/delete")
    @PreAuthorize("@ss.hasPerm('boke:matching:delete')")
    public Result delete(@RequestParam(value = "matchingProductId") Long matchingProductId)  {
        matchingProductService.removeById(matchingProductId);
        return Result.success();
    }

}
